﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Vintner.Items.ModuleData
{
    internal class RadarItemsParam : RadarParameters
    {
        public RadarItemsParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar RadarItemsForm

    internal class RadarItemsForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarItemsForm()
            : base()
        {
            rdrCodeColumn = VI_Items.Item.Name;
            rdrDescColumn = VI_Items.Description.Name;
        }

        protected override Table GetRadarTable()
        {
            return new VI_Items();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", VI_Items.Item);

            qb.Clear();
            qb.AddSelectAll(VI_Items.Name);
            qb.AddFrom(VI_Items.Name);
            qb.AddCompare(VI_Items.Item, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_Items.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(VI_Items.Name);
            qb.AddFrom(VI_Items.Name);
            qb.AddFilter(findQuery);
            qb.AddOrder(VI_Items.Item);


            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string Item = DataGridViewRowValues.GetValue<string>(row, VI_Items.Item);
            return new RadarItemsParam(Item);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarItemsParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion
}